#include "SysRegisterDialog.h"
#include "ui_SysRegisterDialog.h"
#include "SysAssist.h"
#include <QFileDialog>
#include <QDebug>
#include <QDateTime>
#include <QMessageBox>


SysRegisterDialog::SysRegisterDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SysRegisterDialog)
{
    ui->setupUi(this);
//    SysAssist::Center(this);
    timer=new QTimer (this);
    CvOpenCam();
    connect(timer,&QTimer::timeout,this,&SysRegisterDialog::CvNextFrame);
    ui->stackedWidget->setCurrentIndex(0);
}

SysRegisterDialog::~SysRegisterDialog()
{
    delete ui;
    delete timer;
    delete sysOC;
    delete sysDB;
    delete &capture;
    delete &frame;
}

void SysRegisterDialog::closeEvent(QCloseEvent *event)
{
    CvCloseCam();
}


void SysRegisterDialog::CvOpenCam()
{
    capture.open(0);//0，默认摄像头
    if(capture.isOpened()!= true)
    {
        QMessageBox::warning(this,tr("提示"),tr("摄像头打开失败，请检查摄像头"));
    }
    timer->start(33);
}

void SysRegisterDialog::CvNextFrame()
{
    capture.read(frame);//把摄像头获取到的某一帧图像传给 frame
    image = sysOC->Mat2QImage(frame);//Mat2QImage,方法在SysOpenCV.h定义
    QSize size=ui->graphicsView->size();
    QImage mImage = image.scaled(size, Qt::IgnoreAspectRatio);
    QPixmap pix=QPixmap::fromImage(mImage);
    ui->graphicsView->setPixmap(pix);
}

void SysRegisterDialog::CvCloseCam()
{
    timer->stop();
    capture.release();
    ui->graphicsView->clear();
    this->close();
}

/*
 * 通过filename获取身份中路径
 * 调用getIdNumber识别身份证号
 * getStuGrade获取学生年级
 *
 *
 */

void SysRegisterDialog::on_pushButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
//    QString fileName=":/Sys/SysPicture/myIdcard.jpg";
    QString fileName = QFileDialog::getOpenFileName(this,tr("Open Image"),".",tr("Image File(*.png *.jpg *.jpeg *.bmp)"));
    if (fileName.isEmpty())
    {
        QMessageBox::warning(this,QObject::tr("提示"),QObject::tr("未选择图片"));
        return;
    }
    QPixmap *pixmap = new QPixmap(fileName);
    pixmap->scaled(ui->labelIDCard->size(), Qt::KeepAspectRatio);
    ui->labelIDCard->setScaledContents(true);
    ui->labelIDCard->setPixmap(*pixmap);
    QString idNumber=sysOC->getIdNumber(fileName);//通过SysDAL调用百度api识别身份证封装的方法，
    if(idNumber.isEmpty())
    {
       QMessageBox::warning(this,QObject::tr("提示"),QObject::tr("身份证识别失败，请重试！！"));
    }
    else
    {
        //定义并初始化教室编号、年级变量
        int classID = ui->BtnClassId->currentText().left(3).toInt();
        RegInfo info=sysDB->StuSeatReg(classID,idNumber,QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
        QVariant Var;
        Var.setValue(info);
        RegInfo getInfo=Var.value<RegInfo>();
        if(getInfo.Status)
        {
            ui->lineStuId->setText(getInfo.stuId);
            ui->lineName->setText(getInfo.StuName);
            ui->lineSeat->setText(getInfo.seatId);
            ui->lineSex->setText(getInfo.StuSex);
            QString c=getInfo.seatId.left(3);
            int s=getInfo.seatId.right(3).toInt();
            ui->labelSeat->setText( QObject::tr("教室：%1 位置：%2")
                                        .arg(c)
                                        .arg(s)
                                   );
            QMessageBox::warning(this,tr("提示"),QObject::tr("恭喜你 %1 位置登记成功\n座位ID：%2").arg(classID).arg(getInfo.StatusInfo));
        }
        else
        {
            QMessageBox::warning(this,QObject::tr("登记失败"),QObject::tr("原因如下：\n")+getInfo.StatusInfo);
        }
    }
    qDebug()<<"身份证："<<idNumber;
}

void SysRegisterDialog::on_pushButton_2_clicked()
{
    CvCloseCam();
}

void SysRegisterDialog::on_BtnSeatQue_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
    QStringList TableHeader;
    QString id = ui->lineSeatID->text();
    if(id.isEmpty())
    {
        QMessageBox::warning(this,QObject::tr("提示"),QObject::tr("请输入ID"));

    }
    else
    {
        TableHeader <<QObject::tr("登记编号")<<QObject::tr("姓名") << QObject::tr("性别") << QObject::tr("电话")<<QObject::tr("专业")<<QObject::tr("位置编号")<<QObject::tr("登记时间");
        QSqlQueryModel *query = SysDBL::QuerySeat(ui->SeatView,id);
        for(int i = 0 ; i < TableHeader.count(); i++)
        {
            query->setHeaderData(i,Qt::Horizontal,TableHeader[i]);//设置显示框表头显示
        }
        ui->SeatView->setModel(query);
    }

}
